package com.starsoft.frame.fileimport;

import java.text.DecimalFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starsoft.frame.fileimport.pojo.ValueType;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.StringUtil;

public abstract class FileReaderImpl implements FileReader {

	private static Logger logger = LoggerFactory.getLogger(FileReaderImpl.class);
	
	protected String dateFormat = "yyyy-MM-dd";
	protected String numberFormat = "0.0";

	protected Object str2Obj(String valueStr, ValueType type) {
		Object re = null;
		if (type == null) {
			return valueStr;
		}
		try {
			switch (type) {
			case String:
				re = valueStr;
				break;
			case Integer:
				Number int1 = new DecimalFormat(numberFormat).parse(valueStr);
				re = new Integer(int1.intValue());
				break;
			case Double:
				Number doubel1 = new DecimalFormat(numberFormat).parse(valueStr);
				re = new Double(doubel1.doubleValue());
				break;
			case Date:
				re = DateUtil.toDate(valueStr, dateFormat);
				break;
			case DateTime:
				break;
			case Boolean:
				re = Boolean.parseBoolean(valueStr);
				break;
			default:
				break;
			}
			return re;
		} catch (Exception e) {
			logger.error("Parse String {} to Object {} failed {}", valueStr, type.name(), e.getMessage());
			return null;
		}
	}
	
	protected Object str2Obj(String valueStr, ValueType type, String format) {
		Object re = null;
		if (StringUtil.isEmpty(valueStr) || type == null) {
			return null;
		}
		if(StringUtil.isEmpty(format)){
			return str2Obj(valueStr, type);
		}
		try {
			switch (type) {
			case String:
				re = valueStr;
				break;
			case Integer:
				Number int1 = new DecimalFormat(format).parse(valueStr);
				re = new Integer(int1.intValue());
				break;
			case Long:
				Number int2 = new DecimalFormat(format).parse(valueStr);
				re = new Long(int2.longValue());
				break;	
			case Double:
				Number doubel1 = new DecimalFormat(format).parse(valueStr);
				re = new Double(doubel1.doubleValue());
				break;
			case Date:
				re = DateUtil.toDate(valueStr, format);
				break;
			case DateTime:
				re = DateUtil.toDate(valueStr, format);
				break;
			case Boolean:
				if(format.startsWith("!")){
					re = !format.substring(1).equalsIgnoreCase(valueStr);
				}else{
					re = format.equalsIgnoreCase(valueStr);
				}
				break;
			default:
				break;
			}
			return re;
		} catch (Exception e) {
			logger.error("Parse String {} to Object {} failed {}", valueStr, type.name(), e.getMessage());
			return null;
		}
	}
	
	protected ValueType getValueType(Object value) {
		ValueType result = null;
		if (value instanceof String) {
			result = ValueType.String;
		} else if (value instanceof Integer) {
			result = ValueType.Integer;
		} else if (value instanceof Double) {
			result = ValueType.Double;
		} else if (value instanceof Date) {
			result = ValueType.Date;
		} else if (value instanceof Boolean) {
			result = ValueType.Boolean;
		}
		return result;
	}

	
}
